<template>
  <div class="passenger">
    <a-card title="令牌信息"></a-card>
    <a-table :data-source="dataList" :columns="columns" :pagination="false">
      <template #action="{ record }">
        <a-button type="link" size="small" @click="edit(record)">修改令牌余量</a-button>
      </template>
    </a-table>
    <a-pagination
      v-model:current="formObj.page"
      v-model:page-size="formObj.pageSize"
      align="right"
      :total="formObj.total"
      show-size-changer
      style="margin-top: 20px"
      @change="onChange"
    />
    <a-modal
      v-model:open="dialog"
      title="编辑"
      width="700px"
      ok-text="确认"
      cancel-text="取消"
      center
      @ok="handleOk"
      @cancel="cancel"
    >
      <a-form :model="formState">
        <a-form-item label="日期">
          <a-date-picker
            v-model:value="formState.date"
            disabled
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item label="车次编号">
          <TrainSelect v-model:code="formState.trainCode" disabled />
        </a-form-item>
        <a-form-item label="令牌余量">
          <a-input-number v-model:value="formState.count" :min="0" placeholder="请输入令牌余量" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script setup lang="ts">
import { nextTick, onMounted, ref } from 'vue';
import { SkTokenApi } from '@/api/skToken';
import { message } from 'ant-design-vue';
import TrainSelect from '@/components/Train-Select.vue';

const formState = ref<any>({
  id: '',
  date: null,
  trainCode: '',
  count: 0,
});

const dialog = ref(false);

const columns = ref([
  {
    title: '日期',
    dataIndex: 'date',
    key: 'date',
  },
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '令牌余量',
    dataIndex: 'count',
    key: 'count',
  },
  {
    title: '新增时间',
    dataIndex: 'createTime',
    key: 'createTime',
  },
  {
    title: '修改时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    fixed: 'right',
    width: 100,
    slots: { customRender: 'action' },
  },
]);

const formObj = ref({
  date: null,
  trainCode: '',
  page: 1,
  pageSize: 10,
  total: 0,
});

onMounted(() => {
  getList();
});

const dataList = ref([]);

const getList = async (): Promise<void> => {
  const { data } = await SkTokenApi.query(formObj.value);
  dataList.value = data.records || [];
  formObj.value.total = data.total;
};

const onChange = (current: number, pageSize: number): void => {
  console.log(current, pageSize);
  formObj.value.page = current;
  formObj.value.pageSize = pageSize;
  getList();
};

const edit = async (record: any): Promise<void> => {
  console.log('record', record);
  const { data } = await SkTokenApi.detail(record.id);

  formState.value = data;
  nextTick(() => {
    dialog.value = true;
  });
};

const handleOk = async (): Promise<void> => {
  await SkTokenApi.update(formState.value);
  message.success('保存成功');
  dialog.value = false;
  getList();
};

const cancel = (): void => {
  formState.value = {
    id: '',
    date: null,
    trainCode: '',
    count: 0,
  };
};
</script>

<style scoped lang="less">
.passenger {
  :global(.ant-modal .ant-modal-footer) {
    text-align: center;
  }
}
</style>
